﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.Text;

namespace Com.Framework.Data
{
    public class DBArguments : Object, IArguments
    {
        private Hashtable m_HT = null; 
        public DBArguments()
        {
            m_HT = new Hashtable();
        }

        public void SetValue(string key, object value)
        {
            if (m_HT.Contains(key))
            {
                m_HT[key] = value;
            }
            else
            {
                m_HT.Add(key, value);
            }
        }

        public object GetValue(string key, object defaultValue)
        {
            if (m_HT.Contains(key))
            {
                return m_HT[key];
            }

            return defaultValue;
        }

		public T GetValue2<T>(string key, T defaultValue)
		{
			if (m_HT.Contains(key))
			{
				try
				{
					object obj = m_HT[key];

					if (defaultValue is Enum)
					{
						if (obj is string)
							return (T)Enum.Parse(typeof(T), obj.ToString());
						else
							return (T)obj;
					}
					else if (defaultValue is string)
					{
						if (!string.IsNullOrEmpty((string)Convert.ChangeType(obj, typeof(T))))
							return (T)Convert.ChangeType(obj, typeof(T));
					}
					else if (defaultValue == null)
					{
						return (T)obj;
					}
					else
					{
						return (T)Convert.ChangeType(obj, typeof(T));
					}
				}
				catch (Exception)
				{
				}

				return defaultValue;
			}

			return defaultValue;
		}

		public Hashtable GetData()
        {
            return m_HT;
        }
    }
}
